package view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import model.Speaker;
import model.SpeakerListModel;
import tools.NameAutocompletion;
import tools.Translator;

import com.google.inject.Inject;
import com.jidesoft.swing.AutoCompletion;

public class SpeakerList extends JPanel implements ListDataListener {

	private SpeakerListModel speakerListModel;
	private final NameAutocompletion autocompletion;
	private final SpeakerOptionsDialog speakerOptionsDialog;
	private Translator tr;

	@Inject
	public SpeakerList(SpeakerListModel speakerListModel, Translator tr,
			NameAutocompletion autocompletion,
			SpeakerOptionsDialog speakerOptionsDialog) {
		super();

		this.speakerListModel = speakerListModel;
		this.autocompletion = autocompletion;
		this.speakerOptionsDialog = speakerOptionsDialog;
		this.tr = tr;
		
		speakerListModel.addListDataListener(this);
		
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setFocusCycleRoot(true);
	}

	private class SpeakerEntry extends JPanel implements ActionListener {

		private final JTextField nameField;
		private final JButton deleteButton;
		private final JButton speakerOptionsButton;
		private final Speaker speaker;
		
		public SpeakerEntry(final Speaker speaker) {
			
			this.speaker = speaker;
			
			setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
			setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
			nameField = new JTextField(speaker.getName());
			nameField.setColumns(15);
			AutoCompletion autoComplete = new AutoCompletion(nameField,
					autocompletion.getNames());
			autoComplete.setStrict(false);
			autoComplete.setStrictCompletion(false);

			// TODO: Update data according to changes at the speaker
			nameField.addFocusListener(new FocusListener() {

				@Override
				public void focusLost(FocusEvent arg0) {
					// update model value
					speaker.setName(nameField.getText());
				}

				@Override
				public void focusGained(FocusEvent arg0) {
					nameField.selectAll();
				}
			});

			add(nameField);

			speakerOptionsButton = new JButton(tr.tr("DETAILS"));

			speakerOptionsButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					speakerOptionsDialog.showDialogAndSetOptions(speaker);
				}
			});

			add(speakerOptionsButton);
			add(deleteButton = new JButton("X"));
			speakerOptionsButton.setFocusable(false);
			deleteButton.setFocusable(false);
			deleteButton.addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// Delete this entry
			speakerListModel.removeElement(speaker);

		}

	}

	@Override
	public void contentsChanged(ListDataEvent e) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void intervalAdded(ListDataEvent e) {
		if (e.getIndex0() != e.getIndex1()) {
			throw new UnsupportedOperationException();
		}
		Speaker newSpeaker = speakerListModel.get(e.getIndex0());
		SpeakerEntry entry = new SpeakerEntry(newSpeaker);
		add(entry, e.getIndex0());
		revalidate();
		repaint();
	}

	@Override
	public void intervalRemoved(ListDataEvent e) {
		if (e.getIndex0() != e.getIndex1()) {
			throw new UnsupportedOperationException();
		}
		remove(e.getIndex0());
		revalidate();
		repaint();
	}
}
